<?php
declare(strict_types=1);

namespace App\Controller\Admin\V1\PointPosition;

use App\Controller\Admin\AdminController;
use App\Model\PositionSupervise;
use App\Model\PositionSupervisorApply;
use App\Repositories\PositionSuperviseRepository;
use App\Repositories\PositionSupervisorApplyRepository;
use App\Repositories\SupervisorGroupRepository;
use App\Repositories\UserAccountRepository;
use App\Repositories\UserHasPositionRepository;
use App\Repositories\UserRepository;
use App\Repositories\UserWechatRepository;
use App\Request\StoreSupervisorGroupRequest;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;

class SupervisorController extends AdminController
{

    public function list(){
        $rows = $this->request->input('rows',10);
        $start_time = $this->request->input('start_time','');
        $end_time = $this->request->input('end_time','');
        $mobile = $this->request->input('mobile','');
        $wx_nickname = $this->request->input('wx_nickname','');
        $join=[];
        $search=[];
        $search['is_virtual']=[
            'field' => 'is_virtual',
            'value' => 0
        ];
        $search['is_supervisor']=[
            'field' => 'is_supervisor',
            'value' => 1
        ];
        if($start_time!=''){
            $search['start_time']=[
                'field' => 'reg_time',
                'value' => $start_time,
                'method' => '>='
            ];
        }
        if($end_time!=''){
            $search['end_time']=[
                'field' => 'reg_time',
                'value' => $end_time,
                'method' => '<='
            ];
        }
        if($mobile!=''){
            $search['mobile']=[
                'field' => 'mobile',
                'value' => $mobile,
                'method' => 'like'
            ];
        }
        if($wx_nickname!=''){
            $join['user_wechat'] = [
                'on' => ['supervisor_apply.user_id','=','user_wechat.user_id'],
                'where'=>[[
                    'field' => 'wx_nickname',
                    'value' => $wx_nickname,
                    'method' => 'like'
                ]]
            ];
        }
        if($mobile!=''){
            $join['users'] = [
                'on' => ['supervisor_apply.user_id','=','users.id'],
                'where'=>[[
                    'field' => 'mobile',
                    'value' => $mobile,
                    'method' => 'like'
                ]]
            ];
        }
        $orderBy= [
            [
                'field' => 'id',
                'sort' => 'desc'
            ]
        ];
        $column=[
            'users.*'
        ];
        $datas = UserRepository::getInstance()->getList($column,$search,['limit'=>$rows],$orderBy,$join);
        if($datas['result']){
            foreach ($datas['result'] as &$item){
                $item['account'] = UserAccountRepository::getInstance()->one('*',[[
                    'field' => 'user_id',
                    'value' => $item['id']
                ]]);
                $item['wechat'] = UserWechatRepository::getInstance()->one('*',[[
                    'field' => 'user_id',
                    'value' => $item['id']
                ]]);
                $item['count_all'] = PositionSuperviseRepository::getInstance()->count('id',[[
                    'field' => 'user_id',
                    'value' => $item['id'],
                ]]);
                $item['count_done'] = PositionSuperviseRepository::getInstance()->count('id',[[
                    'field' => 'user_id',
                    'value' => $item['id'],
                ],[
                    'field' => 'status',
                    'value' => PositionSupervise::STATUS_DONE
                ]]);
            }
        }
        return $this->response->success($datas);
    }

    public function detail(){
        $id = $this->request->input('id');
        $user = UserRepository::getInstance()->getById($id);
        $user['wechat'] = $user->wechat;
        $user['account'] = $user->account;
        return $this->response->success($user);
    }

    public function applyList(){
        $rows = $this->request->input('rows',10);
        $start_time = $this->request->input('start_time','');
        $end_time = $this->request->input('end_time','');
        $status = $this->request->input('status','all');
        $mobile = $this->request->input('mobile','');
        $wx_nickname = $this->request->input('wx_nickname','');
        $join=[];
        $search=[];
        if($start_time!=''){
            $search['start_time']=[
                'field' => 'created_at',
                'value' => $start_time,
                'method' => '>='
            ];
        }
        if($end_time!=''){
            $search['end_time']=[
                'field' => 'created_at',
                'value' => $end_time,
                'method' => '<='
            ];
        }
        if($status!='all'){
            $search['status']=[
                'field' => 'status',
                'value' => $status
            ];
        }
        if($wx_nickname!=''){
            $join['user_wechat'] = [
                'on' => ['position_supervisor_apply.user_id','=','user_wechat.user_id'],
                'where'=>[[
                    'field' => 'wx_nickname',
                    'value' => $wx_nickname,
                    'method' => 'like'
                ]]
            ];
        }
        if($mobile!=''){
            $join['users'] = [
                'on' => ['position_supervisor_apply.user_id','=','users.id'],
                'where'=>[[
                    'field' => 'mobile',
                    'value' => $mobile,
                    'method' => 'like'
                ]]
            ];
        }
        $orderBy= [
            [
                'field' => 'id',
                'sort' => 'desc'
            ]
        ];
        $column=[
            'position_supervisor_apply.*'
        ];
        $datas = PositionSupervisorApplyRepository::getInstance()->getList($column,$search,['limit'=>$rows],$orderBy,$join);
        if($datas['result']){
            foreach ($datas['result'] as &$item){
                $item['user'] = UserRepository::getInstance()->getBaseInfo($item['user_id']);
            }
        }
        return $this->response->success($datas);
    }

    public function applyConsts(){
        $return=[];
        $status = PositionSupervisorApply::getLabels('status');
        $return['status'] = [];
        foreach ($status as $key=>$val){
            $obj=[
                'name' => $val,
                'value' => $key
            ];
            $return['status']=array_merge($return['status'],[$obj]);
        }
        return $this->response->success($return);
    }

    public function applyChangeStatus(){
        $id = $this->request->input('id');
        $status = $this->request->input('status');
        $apply = PositionSupervisorApplyRepository::getInstance()->getById($id);
        $user = UserRepository::getInstance()->getById($apply->user_id);
        if($status==1){
            if(!$user->is_supervisor){
                DB::beginTransaction();
                try{
                    $apply->status = PositionSupervisorApply::STATUS_SUCCESS;
                    if(!$apply->save()){
                        throw new \Exception('申请状态保存失败');
                    }
                    $user->is_supervisor=1;
                    $user->be_supervisor_time = date('Y-m-d H:i:s');
                    if(!$user->save()){
                        throw new \Exception('监督员状态更新失败');
                    }
                    DB::commit();
                }catch (\Throwable $exception){
                    DB::rollBack();
                    return $this->response->error(10012,$exception->getMessage());
                }
            }
        }
        if($status==-1){
            $apply->status = PositionSupervisorApply::STATUS_FAIL;
            if(!$apply->save()){
                return $this->response->error(10012,'保存失败');
            }
        }
        return $this->response->success([]);
    }

    public function ownPosition(){
        $user_id = $this->request->input('user_id',0);
        if(!$user_id){
            return $this->response->error(10414,'请输入用户id');
        }
        $rows = $this->request->input('rows',10);
        $join=[];
        $search=[];
        $search['user_id']=[
            'field' => 'user_id',
            'value' => $user_id
        ];
        $join = [
            'point_position' => [
                'on' => ['user_has_position.position_id','=','point_position.id']
            ]
        ];
        $orderBy= [
            [
                'field' => 'user_has_position.id',
                'sort' => 'desc'
            ]
        ];
        $column=[
            'point_position.*'
        ];
        $datas = UserHasPositionRepository::getInstance()->getList($column,$search,['limit'=>$rows],$orderBy,$join);
        return $this->response->success($datas);
    }

    public function groupList(){
        $rows = $this->request->input('rows',10);
        $search=[];
        $orderBy= [
            [
                'field' => 'id',
                'sort' => 'asd'
            ]
        ];
        $datas = SupervisorGroupRepository::getInstance()->getList('*',$search,['limit'=>$rows],$orderBy);
        return $this->response->success($datas);
    }

    public function groupStore(StoreSupervisorGroupRequest $request){
        $params = $request->all();
        $result = SupervisorGroupRepository::getInstance()->store($params);
        if($result['success']){
            return $this->response->success([
                'id' => $result['data']->id
            ]);
        }
        return $this->response->error(10000,$result['msg']);
    }

    public function groupDelete(){

    }

}